package org.termine.jsf;

import java.io.Serializable;

import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.ViewScoped;

import org.termine.entity.Location;
import org.termine.entity.PersonalData;
import org.termine.logic.LocationManager;
import org.termine.logic.PersonalDataManager;

import org.primefaces.model.map.DefaultMapModel;
import org.primefaces.model.map.LatLng;
import org.primefaces.model.map.MapModel;
import org.primefaces.model.map.Marker;

@ManagedBean
@ViewScoped
public class PersonalDataJSF extends BaseJSF implements Serializable {

	private static final long serialVersionUID = -1352747632642616139L;

	@ManagedProperty(value = "#{personalDataManager}")
	private PersonalDataManager personalDataManager;
	@ManagedProperty(value = "#{loginJSF}")
	private LoginJSF loginJSF;
	@ManagedProperty(value = "#{locationManager}")
	private LocationManager locationManager;

	private MapModel mapModel;
	private PersonalData personalData;
	private Location location;
	private String confirmPassword;
	private String oldPassword;
	private String newPassword;

	/*
	 * Init
	 */
	@PostConstruct
	public void init() {
		personalData = getLoginJSF().getPersonalData().copy();
		location = getLoginJSF().getLocation().copy();

		mapModel = new DefaultMapModel();
		mapModel.addOverlay(new Marker(new LatLng(location.getLatitude(), location.getLongitude()),
				"my location"));
	}

	/*
	 * Jsf Action Methods
	 */
	public String updatePersonalData() {
		if (getPersonalDataManager().updatePersonalData(personalData)) {
			getLoginJSF().setPersonalData(personalData);
			setInfoMessage("personal data changed successfully");
			return "SUCCESS";
		} else {
			setFatalMessage("changing personal data failed");
			return "ERROR";
		}
	}

	public String updateLocation() {
		if (getLocationManager().updateLocation(location, getLoginJSF().getPersonalData())) {
			getLoginJSF().setLocation(location);
			setInfoMessage("Location changed successfully!");
			return "SUCCESS";
		} else {
			setFatalMessage("Changing location failed! An error occurred.");
			return "ERROR";
		}
	}

	public String updatePassword() {
		if (newPassword.equals(confirmPassword)
				&& oldPassword.equals(getLoginJSF().getPersonalData().getPassword())) {
			getLoginJSF().getPersonalData().setPassword(newPassword);
			getPersonalData().setPassword(newPassword);
			if (getPersonalDataManager().updatePersonalData(getPersonalData())) {
				setInfoMessage("password changed successfully");
				return "SUCCESS";
			}
		}
		setErrorMessage("changing password failed");
		return "ERROR";
	}

	/*
	 * Getters & Setters
	 */
	public MapModel getMapModel() {
		return mapModel;
	}

	public PersonalData getPersonalData() {
		return personalData;
	}

	public String getNewPassword() {
		return newPassword;
	}

	public void setNewPassword(String newPassword) {
		this.newPassword = newPassword;
	}

	public String getConfirmPassword() {
		return confirmPassword;
	}

	public void setConfirmPassword(String confirmPassword) {
		this.confirmPassword = confirmPassword;
	}

	public String getOldPassword() {
		return oldPassword;
	}

	public void setOldPassword(String oldPassword) {
		this.oldPassword = oldPassword;
	}

	public void setPersonalData(PersonalData personalData) {
		this.personalData = personalData;
	}

	public Location getLocation() {
		return location;
	}

	public void setLocation(Location location) {
		this.location = location;
	}

	public PersonalDataManager getPersonalDataManager() {
		return personalDataManager;
	}

	public void setPersonalDataManager(PersonalDataManager personalDataManager) {
		this.personalDataManager = personalDataManager;
	}

	public LoginJSF getLoginJSF() {
		return loginJSF;
	}

	public void setLoginJSF(LoginJSF loginJSF) {
		this.loginJSF = loginJSF;
	}

	public LocationManager getLocationManager() {
		return locationManager;
	}

	public void setLocationManager(LocationManager locationManager) {
		this.locationManager = locationManager;
	}

}
